<?php

namespace app\api\controller;
use app\common\controller\Api;
use think\facade\Config;
use app\Request;
use think\facade\Db;

class Doctor extends Api{
    protected $noNeedLogin = ['doctorlist','reservationnum','selectevaltion'];
    public function doctorlist(){
        $list = Db::name('doctor')
            -> where('status',1) -> select() -> toArray();
        //已帮助
        $ywc = Db::name('reservation_item')
            -> where('status',3)
            -> field('id,doctorid1') -> select();
        foreach ( $ywc as $kk => $vv){
            foreach ($list  as $k1 => $v1){
                if($v1['id'] == $vv['doctorid1']){
                    $list[$k1]['ywc'] += 1; //
                }
            }
        }
        $data = ['list' =>$list];
        $this->success('请求成功',$data);
    }

    public function doctordsdetails(){
        $request = $this->request->request();
        $res['detalis'] = Db::name('doctor')
            -> where('id',$request['doctorid'])
            -> find();
//        if($res){
//            Db::name('doctor') -> where('id',$request['doctorid']) -> inc('review',1) -> update();
//        }
        $res['re_day'] = Config::get("site.re_day") ?? 30;
        $rewhere = 'status = 1 or status = 2';
        $reservation_item = Db::name('reservation_item')
            -> where(['r_ordersn'=>$request['r_ordersn'],'doctorid1'=>$request['doctorid'],'uid'=>$this->uid])
            -> where($rewhere)
            -> field('id,status')
            -> find();
        $res['yuyue'] = [];
        if(!empty($reservation_item)){
            $res['yuyue'] =$reservation_item;
        }

        $res['evalcount'] = Db::name("doctor_evaluation") -> where("evalid",$request['doctorid']) -> count();
        //查询医生评价 '%Y-%m-%d %H:%i:%s'
        $res['evaluatlist'] = Db::name("doctor_evaluation")
            -> alias('de')
            -> leftjoin('user u','u.id=de.uid')
            -> where("evalid",$request['doctorid'])
            -> field('de.id,de.uid,de.content,FROM_UNIXTIME(de.createtime,\'%Y-%m-%d\') as dcreatetime,u.nickname,u.avatar')
            -> order('de.createtime desc')
            ->limit(0,4)
            -> select();
        $res['balance'] = Db::name('user') -> where('id', $this -> uid) -> value('balance');
        $this->success('请求成功',$res);
    }


    //查询数据
    public function reservationnum(){
        $request = $this->request->request();
        $doctorid = $request['doctorid'];
        $year_month =  $request['year_month'];

//        $uid =  $request -> post('uid');
        if(!$doctorid) $this -> error("参数不能为空");
        $res['info'] = Db::name('reservation') -> where(['doctorid'=>$doctorid,'reservationtime'=>$year_month]) -> select() -> toArray();
        $res['re_period'] = Db::name('doctor_sku')
            -> where(['doctorid'=>$doctorid,'status' => 1])
            -> field('id,period as dateperiod,pn as n,pnumber')
            -> select() -> toArray();
//        foreach ($res['re_period'] as $key => $val){
//            $res['re_period'][$key]['isShow'] = 'true';
//            foreach ($res['info'] as $kk => $vv){
//                if($vv['reservation_period'] == $val['dateperiod']){
//                    echo  3333;
////                    $pnum = (int)$val['pnumber'] - (int)$val1['reservationsnum'];
//                    $res['re_period'][$key]['pnumber'] = (int)$val['pnumber'] - (int)$vv['reservationsnum'];
//                }else{
//                    echo 4444;
//                    $res['re_period'][$key]['pnumber'] = (int)$val['pnumber'];
//                }
//            }
//        }
        $this->success('请求成功',$res);
    }



    public function selectevaltion(){
        $request = $this->request->request();
//        $pageSize = $request -> get('num')['num'] ?? 15;
//        $page = $request -> get('page')['page'] ?? 1;
//        $pages = ($page - 1) * $pageSize;
        if ($request['evalid'] == '') $this -> error('参数错误');
        $res = Db::name('doctor_evaluation')
            -> alias('de')
            -> leftjoin('user u','u.id=de.uid')
            -> where('evalid',$request['evalid'])
            -> field('de.content,FROM_UNIXTIME(de.createtime,\'%Y-%m-%d\') as dcreatetime,u.nickname,u.avatar')
            -> order('de.createtime desc')
            -> limit(0,15)
            -> select();
        $this->success('请求成功',$res);
    }

}